package cn.iocoder.yudao.module.ai.service.modelfactoryparam;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.modelfactoryparam.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.modelfactoryparam.ModelFactoryParamDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.modelfactoryparam.ModelFactoryParamMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 业务模型参数 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class ModelFactoryParamServiceImpl implements ModelFactoryParamService {

    @Resource
    private ModelFactoryParamMapper modelFactoryParamMapper;

    @Override
    public Integer createModelFactoryParam(ModelFactoryParamSaveReqVO createReqVO) {
        // 插入
        ModelFactoryParamDO modelFactoryParam = BeanUtils.toBean(createReqVO, ModelFactoryParamDO.class);
        modelFactoryParamMapper.insert(modelFactoryParam);
        // 返回
        return modelFactoryParam.getId();
    }

    @Override
    public void updateModelFactoryParam(ModelFactoryParamSaveReqVO updateReqVO) {
        // 校验存在
        validateModelFactoryParamExists(updateReqVO.getId());
        // 更新
        ModelFactoryParamDO updateObj = BeanUtils.toBean(updateReqVO, ModelFactoryParamDO.class);
        modelFactoryParamMapper.updateById(updateObj);
    }

    @Override
    public void deleteModelFactoryParam(Integer id) {
        // 校验存在
        validateModelFactoryParamExists(id);
        // 删除
        modelFactoryParamMapper.deleteById(id);
    }

    private void validateModelFactoryParamExists(Integer id) {
        if (modelFactoryParamMapper.selectById(id) == null) {
            throw exception(MODEL_FACTORY_PARAM_NOT_EXISTS);
        }
    }

    @Override
    public ModelFactoryParamDO getModelFactoryParam(Integer id) {
        return modelFactoryParamMapper.selectById(id);
    }

    @Override
    public PageResult<ModelFactoryParamDO> getModelFactoryParamPage(ModelFactoryParamPageReqVO pageReqVO) {
        return modelFactoryParamMapper.selectPage(pageReqVO);
    }

}